package nettypro.netty.heartbeat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.EventLoopGroup;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.concurrent.Future;

import java.util.concurrent.Callable;

/**
 * @ClassName MyServerHandler
 * @Description TODO
 * @Author zeny
 * @Date 2020/3/25 0025 10:08
 */
public class MyServerHandler extends ChannelInboundHandlerAdapter {
    EventLoopGroup group = new DefaultEventLoopGroup(10);
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        Future<Object> f = group.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                System.err.println("线程池任务");
                System.out.println(Thread.currentThread().getName());
                return true;
            }
        });
        f.await();
        System.out.println(f.isSuccess());
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            String eventType = null;
            switch (event.state()) {
                case READER_IDLE:
                    eventType = "读空闲";
                    break;
                case WRITER_IDLE:
                    eventType = "写空闲";
                    break;
                case ALL_IDLE:
                    eventType = "读写空闲";
                    break;
                default:
                    break;
            }
            System.out.println(ctx.channel().remoteAddress() +"--超时时间--"+ eventType);
            System.out.println("服务器空闲处理");

//            ctx.close();
        }
    }
}
